package translinkApp.Activities;

import java.util.ArrayList;

import translinkApp.Adaptors.LocationsAdapter;
import translinkApp.Adaptors.Sections.SectionedCheckBoxAdaptor;
import translinkApp.Data.ResolvedLocation.ResolvedLocation;
import translinkApp.Data.ResolvedLocation.ResolvedLocationList;
import translinkApp.DialogBoxes.ResolveLocation.ResolveLocationDialog;
import translinkApp.DialogBoxes.ResolveLocation.ResolveLocationDialogData;
import translinkApp.Utilities.DataResource;
import translinkApp.views.activity.DialogGenerator;
import translinkApp.views.activity.DialogGeneratorList;
import translinkApp.views.activity.DialogLoaderActivity;
import translinkApp.views.dialog.LachDialog;
import translinkApp.views.dialog.LachDialogArguments;
import translinkApp.views.dialog.LachDialogData;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

public class LocationsMenuActivity extends DialogLoaderActivity {

	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.locationsmenu);

		listView = (ListView) findViewById(R.id.modifyLocationsListView);

		Button addButton = (Button) findViewById(R.id.modifyLocationAdd);
		addButton.setOnClickListener(addButtonListener);

		Button backButton = (Button) findViewById(R.id.modifyLocationBack);
		backButton.setOnClickListener(backButtonListener);

		Button deleteButton = (Button) findViewById(R.id.modifyLocationDelete);
		deleteButton.setOnClickListener(deleteButtonListener);

		updateAdaptor();

	}

	OnClickListener addButtonListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			showLachDialog(0, new LachDialogArguments(false));
		}
	};

	@Override
	public void dialogGenerator(DialogGeneratorList list) {
		list.addDialog(new DialogGenerator() {

			@Override
			public LachDialog createDialog(LachDialogArguments args, LachDialogData data) {
				final ResolveLocationDialog locationDialog = new ResolveLocationDialog(
						LocationsMenuActivity.this, args, (ResolveLocationDialogData) data);

				locationDialog.setOnDismissListener(new OnDismissListener() {

					@Override
					public void onDismiss(DialogInterface dialog) {

						ResolvedLocation location = locationDialog.getResolvedLocation();
						if (location != null) {
							getResolvedLocations().add(location);
							updateAdaptor();

							Toast.makeText(LocationsMenuActivity.this, "Location added",
									Toast.LENGTH_SHORT).show();
						}

					}
				});

				return locationDialog;
			}
		});
	}

	OnClickListener backButtonListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};

	OnClickListener deleteButtonListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			ArrayList<CheckBox> boxes = getLocationCheckBoxes();

			boolean itemDeleted = false;
			for (int i = 0; i < boxes.size(); i++) {
				CheckBox box = boxes.get(i);
				ResolvedLocation deleteLocation = (ResolvedLocation) box.getTag();
				if (box.isChecked()) {

					getResolvedLocations().remove(deleteLocation);
					itemDeleted = true;
				}
			}

			if (itemDeleted) {
				Toast.makeText(LocationsMenuActivity.this, "Location(s) deleted",
						Toast.LENGTH_SHORT).show();
			}

			updateAdaptor();
		}
	};

	private ResolvedLocationList getResolvedLocations() {
		return DataResource.savedLocations.getData();
	}

	private void updateAdaptor() {
		DataResource.savedLocations.save(this);

		ResolvedLocationList suburbLocations = new ResolvedLocationList();
		ResolvedLocationList streetLocations = new ResolvedLocationList();

		for (ResolvedLocation location : getResolvedLocations()) {
			if (location.isStreet()) {
				streetLocations.add(location);
			} else {
				suburbLocations.add(location);
			}

		}

		suburbLocations.sort();
		streetLocations.sort();

		SectionedCheckBoxAdaptor sectionCheckBox = new SectionedCheckBoxAdaptor(this);

		if (suburbLocations.size() > 0) {

			LocationsAdapter suburbAdapter = new LocationsAdapter(this, suburbLocations);
			sectionCheckBox.addSection("Suburb / Landmark", suburbAdapter);
		}

		if (streetLocations.size() > 0) {

			LocationsAdapter streetAdapter = new LocationsAdapter(this, streetLocations);
			sectionCheckBox.addSection("Street", streetAdapter);
		}

		listView.setAdapter(sectionCheckBox);
	}

	private final int MENU_SELECT_ALL = 1;
	private final int MENU_DESELECT_ALL = 2;

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem selectAllItem = menu.add(0, MENU_SELECT_ALL, 0, "Select All");
		selectAllItem.setIcon(android.R.drawable.ic_menu_agenda);

		MenuItem deselectAllItem = menu.add(0, MENU_DESELECT_ALL, 0, "Deselect All");
		deselectAllItem.setIcon(android.R.drawable.ic_menu_close_clear_cancel);

		return true;
	}

	private ArrayList<CheckBox> getLocationCheckBoxes() {
		SectionedCheckBoxAdaptor adaptor = ((SectionedCheckBoxAdaptor) listView.getAdapter());

		return adaptor.GetAllItems();
	}

	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case MENU_SELECT_ALL:

			for (CheckBox box : getLocationCheckBoxes()) {
				box.setChecked(true);
			}
			return true;

		case MENU_DESELECT_ALL:
			for (CheckBox box : getLocationCheckBoxes()) {
				box.setChecked(false);
			}
			return true;

		}
		return false;
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);

		recreateDialog(savedInstanceState);

	}

}
